package nl.windgazer.arachne.components.yql;

import nl.windgazer.arachne.services.yql.YQLConduit;

import org.apache.tapestry5.Block;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Loop;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONArray;
import org.apache.tapestry5.json.JSONObject;

/**
 * This Component attempts to use YQL to grab a set of photo's by the title of the set and the name of a user.
 * 
 * @author mreuring
 *
 */
public class YQLFlickrPhotos {

	//Injects and Persists

	@Inject
	private YQLConduit yc;

	//Properties and Parameters

	private JSONObject[] photos = null;

	private JSONObject photoq;

	private JSONObject infoq;

	@SuppressWarnings("unused")
	@Property
	private JSONObject photo;

	@Inject
	@Property
	private Block defaultPhoto;

	@Parameter(required=false)
	private Block photoDisplay;

	//Components

	@SuppressWarnings("unused")
	@Component(id = "photos", parameters = { "source=photos",
			"value=photo" })
	private Loop _photos;

	//Events

	//Other (check for business-logic, it should not remain here)

	public JSONObject[] getPhotos() {
		if (photos==null) {
			photoq = yc.jsonByQuery("SELECT * FROM flickr.people.publicphotos(0,2) WHERE user_id IN (SELECT id FROM flickr.people.findbyusername WHERE username='windgazer') AND extras='url_sq,url_m,url_t,url_s,url_l'");
			final int count = photoq.getInt("count");
			if (count==0) {
				return null;
			}
			//This second query is required to obtain the link-back URL which is required by FlickR policies when showing a picture from their servers...
			infoq = yc.jsonByQuery("SELECT id, urls FROM flickr.photos.info WHERE photo_id IN (SELECT id FROM flickr.people.publicphotos(0,2) WHERE user_id IN (SELECT id FROM flickr.people.findbyusername WHERE username='windgazer'))");
			final JSONArray pa = photoq.getJSONObject("results").getJSONArray("photo");
			photos = new JSONObject[pa.length()];
			for (int i = 0; i < pa.length(); i++) {
				final JSONObject photo = pa.getJSONObject(i);
				final JSONObject info = getById(infoq, photo.getString("id"));
				if (info!=null) photo.put("photopage", info.getJSONObject("urls").getJSONObject("url").get("content"));
				photos[i] = photo;
			}
		}
		return photos;
	}

	/**
	 * This is a helper method intended to obtain photo-info from a query to the flickr.photos.info table by id
	 * of a specific photo. The intention is to locally get specific photo info while throwing a single query
	 * at YQL for an entire 'set', this to keep the amount of queries to a minimum...
	 * 
	 * @param q
	 * @param id
	 * @return
	 */
	public static JSONObject getById(JSONObject q, String id) {
		JSONArray objects = q.getJSONObject("results").getJSONArray("photo");
		for (int i = 0; i < objects.length(); i++) {
			final JSONObject o = objects.getJSONObject(i);
			final String oid = o.getString("id");
			if (oid!=null && oid.equals(id)) return o;
		}
		return null;
	}

	public Block getPhotoBlock() {
		if (photoDisplay!=null) {
			return photoDisplay;
		} else {
			return defaultPhoto;
		}
	}

}
